package oakclub.entity;

import oakclub.global.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class ResponseEntity extends BaseEntity
{
    private ErrorCode errorCode;
    private String message;
    private Map<String, Object> data;

    public enum ErrorCode
    {
        SUCCESS(0), FAIL(-1);

        private final int val;

        private ErrorCode(int val)
        {
            this.val = val;
        }

        public int val()
        {
            return this.val;
        }
    }

    private static final Logger logger = LoggerFactory.getLogger(ResponseEntity.class);

    public ResponseEntity(ErrorCode errorCode)
    {
        this.errorCode = errorCode;
        this.message = "";
        this.data = new HashMap<>();
    }

    public void setErrorCode(ErrorCode errorCode)
    {
        this.errorCode = errorCode;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public void put(String key, Object value)
    {
        this.data.put(key, value);
    }

    public String toJson()
    {
        String json = Json.getParser().toJson(this).replaceAll(":null", ":\"\"");
        logger.debug(json);
        return json;
    }

    @Override
    public String toString()
    {
        return this.toJson();
    }
}
